package com.tyk.bookstore.back.task.schedule;

import com.tyk.bookstore.back.comment.service.CommentApiService;
import com.tyk.bookstore.back.order.service.OrderDetailApiService;
import com.tyk.bookstore.back.product.model.query.Statistic4TaskUpdateQuery;
import com.tyk.bookstore.back.product.service.BookStatisticApiService;
import com.tyk.bookstore.back.product.service.CollectApiService;
import com.tyk.bookstore.back.product.service.PraiseApiService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tyk
 * @description BookTask
 */
@Service
@RequiredArgsConstructor
public class BookTask {

    private final BookStatisticApiService statisticApiService;
    private final OrderDetailApiService detailApiService;
    private final CollectApiService collectApiService;
    private final PraiseApiService praiseApiService;
    private final CommentApiService commentApiService;


    /**
     * 刷新书籍评收藏数据
     */
    public void flushCollectNum() {
        List<Statistic4TaskUpdateQuery> qs = collectApiService
                .getCollect4TaskDtoList()
                .stream().map(it -> {
                    Statistic4TaskUpdateQuery q = new Statistic4TaskUpdateQuery();
                    q.setBookId(it.getBookId()).setCollectNum(it.getNum());
                    return q;
                }).toList();
        statisticApiService.updateStatistic(qs);
    }

    /**
     * 刷新书籍购买人数数据
     */
    public void flushPayNum() {
        List<Statistic4TaskUpdateQuery> qs = detailApiService
                .getOrderPayDetail4TaskDtoList()
                .stream().map(it -> {
                    Statistic4TaskUpdateQuery q = new Statistic4TaskUpdateQuery();
                    q.setBookId(it.getBookId());
                    q.setPayNum(it.getOrderNum());
                    return q;
                }).toList();
        statisticApiService.updateStatistic(qs);
    }

    /**
     * 刷新书籍销量
     */
    public void flushSaleNum() {
        List<Statistic4TaskUpdateQuery> qs = detailApiService
                .getOrderSaleDetail4TaskDtoList()
                .stream().map(it -> {
                    Statistic4TaskUpdateQuery q = new Statistic4TaskUpdateQuery();
                    q.setBookId(it.getBookId());
                    q.setSaleNum(it.getSaleNum());
                    return q;
                }).toList();
        statisticApiService.updateStatistic(qs);
    }

    /**
     * 刷新书籍评论数量
     */
    public void flushCommentNum() {
        List<Statistic4TaskUpdateQuery> qs = commentApiService
                .getCommentNum4TaskDtoList()
                .stream().map(it -> {
                    Statistic4TaskUpdateQuery q = new Statistic4TaskUpdateQuery();
                    q.setBookId(it.getBookId());
                    q.setCommentNum(it.getCommentNum());
                    return q;
                }).toList();
        statisticApiService.updateStatistic(qs);
    }

    /**
     * 刷新书籍点赞数量
     */
    public void flushLikeNum() {
        List<Statistic4TaskUpdateQuery> qs = praiseApiService
                .getPraise4TaskDtoList()
                .stream().map(it -> {
                    Statistic4TaskUpdateQuery q = new Statistic4TaskUpdateQuery();
                    q.setBookId(it.getBookId());
                    q.setLikeNum(it.getLikeNum());
                    return q;
                }).toList();
        statisticApiService.updateStatistic(qs);
    }

}
